/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAL;

import Entity.Location;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author MrKoi
 */
public class LocationBean {
    Collection listMC,listEdit;
    Connect c=new Connect();
    Connection con;
    CallableStatement cl=null;
    public LocationBean() {
    }
     private void closeCnn(){
        try {
            cl.close();
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(AdminBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public boolean CreateLocationname(String nameCity)
    {
        con=c.getConnection();
       try
       {
           cl=con.prepareCall("{call CreateLocationname_sp(?)}");
           cl.setString(1,nameCity);
           int rs=cl.executeUpdate();
           if(rs >0)
           {
               return true;
           }
          closeCnn();
       }catch (Exception ex){closeCnn();return false;}
        return false;
    }
     public Collection ListLocation()
    {
           con=c.getConnection();
        listMC=new ArrayList();
        try
        {
            cl=con.prepareCall("{call ListLocation_sp}");
            ResultSet rs=cl.executeQuery();
            while(rs.next())
            {
                Location lc=new Location();
                lc.setLocationID(rs.getInt("LocationID"));
                lc.setNamecity(rs.getString("Namecity"));   
                listMC.add((Object)lc);
            }
            closeCnn();
        }catch (Exception ex){closeCnn();}
            return listMC;
    }
     //edit drop
      public Collection SelectLocationEdit(String namecity)
    {
           con=c.getConnection();
        listEdit=new ArrayList();
        try
        {
            cl=con.prepareCall("{call SelectLocationEdit_sp(?)}");
            cl.setString("name", namecity);
            ResultSet rs=cl.executeQuery();
            while(rs.next())
            {
                Location lc=new Location();
                if(rs.getString("Namecity").equals(namecity))
                {
                 lc.setEditDrop("selected");
                }
                else
                {
                  lc.setEditDrop("");
                }
                lc.setLocationID(rs.getInt("LocationID"));
                lc.setNamecity(rs.getString("Namecity"));  
                listEdit.add((Object)lc);
            }
           closeCnn();
        }catch (Exception ex){closeCnn();}
            return listEdit;
    }
      //delete location
    public boolean DeleteLocationName(int id)
    {
        con=c.getConnection();
        try
        {
            cl=con.prepareCall("{call DeleteLocationName_sp(?)}");
            cl.setInt(1,id);
            int rs=cl.executeUpdate();
            if(rs >0)
            {
                return true;
            }
            closeCnn();
        }catch (Exception ex){closeCnn();return false;}
        return false;
    }
}
